﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class MasterPage3 : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DisplayLeftContent();
            //this.SiteMapDataSource1.Provider = SiteMap.Providers["AdministratorsSiteMap"];
            //Check();            
        }
    }

    protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        //获取当前节点
        TreeNode currentNode = e.Node;
        Session[currentNode.Value] = currentNode.Value;
    }

    protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
    {
        TreeNode currentNode = e.Node;
        //移除存储的节点
        Session.Remove(currentNode.Value);
    }

    protected void TreeView1_DataBound(object sender, EventArgs e)
    {
        this.TreeView1.TreeNodeExpanded -= new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
        this.TreeView1.TreeNodeCollapsed -= new TreeNodeEventHandler(TreeView1_TreeNodeCollapsed);
        foreach (TreeNode tn in this.TreeView1.Nodes)
        {
            //若循环节点的文本与当前节点的文本匹配,展开该节点；反之折叠
            if (Session[tn.Value] != null)
            {
                tn.Expand();
                foreach (TreeNode tn1 in tn.ChildNodes)
                {
                    if (Session[tn1.Value] != null)
                        tn1.Expand();
                    foreach (TreeNode tn2 in tn1.ChildNodes)
                    {
                        if (Session[tn2.Value] != null)
                            tn2.Expand();
                        foreach (TreeNode tn3 in tn2.ChildNodes)
                        {
                            if (Session[tn3.Value] != null)
                                tn3.Expand();
                        }
                    }
                }
            }
            else
            {
                tn.Collapse();
            }
        }
        this.TreeView1.TreeNodeCollapsed += new TreeNodeEventHandler(TreeView1_TreeNodeCollapsed);
        this.TreeView1.TreeNodeExpanded += new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DisplayLeftContent1();
    }

    protected void DisplayLeftContent1()
    {
        if (this.Panel1.Visible)
        {
            this.Panel1.Visible = false;
            Session["LeftContent"] = "1";
            this.rightContent.Style.Remove("width");
            this.rightContent.Style.Add("width", "99.5%");
        }
        else
        {
            this.Panel1.Visible = true;
            Session.Remove("LeftContent");
            this.rightContent.Style.Remove("width");
            this.rightContent.Style.Add("width", "87.5%");
        }
    }

    private void DisplayLeftContent()
    {
        if (Session["LeftContent"] == null)
        {
            this.Panel1.Visible = true;
            this.rightContent.Style.Remove("width");
            this.rightContent.Style.Add("width", "87.5%");
        }
        else
        {
            this.Panel1.Visible = false;
            this.rightContent.Style.Remove("width");
            this.rightContent.Style.Add("width", "99.5%");
        }
    }



    private void Check()
    {
        if (HttpContext.Current != null)
        {
            string[] roles = Roles.GetRolesForUser();
            if (roles.Length == 0)
            {
                this.SiteMapDataSource1.Provider = SiteMap.Providers["AnonymousSiteMap"];
            }
            else
            {
                switch (roles[0])
                {
                    case "Operator":
                        this.SiteMapDataSource1.Provider = SiteMap.Providers["OperatorSiteMap"];
                        break;
                    case "Packer":
                        this.SiteMapDataSource1.Provider = SiteMap.Providers["PackerSiteMap"];
                        break;
                    case "IT Engineer":
                        this.SiteMapDataSource1.Provider = SiteMap.Providers["ITEngineerSiteMap"];
                        break;
                    case "Quality Engineer":
                        this.SiteMapDataSource1.Provider = SiteMap.Providers["QualityEngineerSiteMap"];
                        break;
                    case "Manager":
                        this.SiteMapDataSource1.Provider = SiteMap.Providers["ManagerSiteMap"];
                        break;
                    case "Administrators":
                        this.SiteMapDataSource1.Provider = SiteMap.Providers["AdministratorsSiteMap"];
                        break;
                    default:
                        this.SiteMapDataSource1.Provider = SiteMap.Providers["OperatorSiteMap"];
                        break;
                }
            }
            //if(Roles.IsUserInRole("Operator"))
            //{
            //    this.SiteMapDataSource1.Provider = SiteMap.Providers["OperatorSiteMap"];
            //}
            //if (Roles.IsUserInRole("Packer"))
            //{
            //    this.SiteMapDataSource1.Provider = SiteMap.Providers["PackerSiteMap"];
            //}
            //if (Roles.IsUserInRole("IT Engineer"))
            //{
            //    this.SiteMapDataSource1.Provider = SiteMap.Providers["ITEngineerSiteMap"];
            //}
            //if (Roles.IsUserInRole("Quality Engineer"))
            //{
            //    this.SiteMapDataSource1.Provider = SiteMap.Providers["QualityEngineerSiteMap"];
            //}
            //if (Roles.IsUserInRole("Manager"))
            //{
            //    this.SiteMapDataSource1.Provider = SiteMap.Providers["ManagerSiteMap"];
            //}
            //if (Roles.IsUserInRole("Administrators"))
            //{
            //    this.SiteMapDataSource1.Provider = SiteMap.Providers["AdministratorsSiteMap"];
            //}
        }
    }
}
